#pragma once
#include <iostream>
#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <cstdlib>
namespace ns_sock
{
    enum {
        SOCK_ERROR = 2,
        BIND_ERROR,
        LISTEN_ERROR
    };

    const int g_backlog = 5;

    class Sock {
        public:
            static int Socket() {
                int sock = socket(AF_INET, SOCK_STREAM, 0);
                if (sock < 0) {
                    std::cerr << "sock error" << std::endl;
                    exit(SOCK_ERROR);
                }
                return sock;
            }
            static void Bind(const int &sock, const u_int16_t &port) {
                struct sockaddr_in local;
                bzero(&local, sizeof(local));
                local.sin_family = AF_INET;
                local.sin_port = htons(port);
                local.sin_addr.s_addr = INADDR_ANY;
                if (bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0) {
                    std::cerr << "bind error" << std::endl;
                    exit(BIND_ERROR);
                }
            }
            static void Listen(const int &sock) {
                if (listen(sock, 5) < 0) {
                    std::cerr << "listen error" << std::endl;
                    exit(LISTEN_ERROR);
                }
            }
            static int Accept() {}
    };
} // namespace ns_sock
